# -*- coding: utf-8 -*-
from django import forms
from django.utils.translation import ugettext as _

from models import WebComic, Strip
from widgets import ForumFileWidget

class WebComicForm(forms.ModelForm):
    thumbnail = forms.ImageField(widget=ForumFileWidget)
    image = forms.ImageField(widget=ForumFileWidget)
    
    def clean_thumbnail(self):
        data = self.cleaned_data["thumbnail"]
        if isinstance( data, unicode ):
            return data
        
        image_data = data.read()
        
        from StringIO import StringIO
        from PIL import Image
        
        # validating image lenght
        if len(image_data)/1024 > 40:
            raise forms.ValidationError("Thumbnail's image size is too big. Max is 60kb")
        
        #image = Image.open( StringIO(image_data) )
        #width, height = image.size
        
        # validating image width and height
        #if width!= 60 or height!=60:
        #    raise forms.ValidationError("Thumbnail width and height should be 60px")
        
        return data
    
    class Meta:
        model = WebComic

class ComicStripForm(forms.ModelForm):
    image = forms.ImageField(widget=ForumFileWidget)
    
    class Meta:
        model = Strip
        exclude = ("page", "prev", "next")